package com.jy.rhin.application.cda;

import com.jy.api.hospital.HospitalAPI;
import com.jy.api.hospital.dto.HospitalDTO;
import com.jy.rhin.domain.cda.CDACreator;
import com.jy.rhin.infra.cda.CDAException;
import com.jy.rhin.infra.cda.service.ChanQianSFService;
import com.jy.rhin.infra.cda.ycf.ChanQianSFVo;
import com.jy.rhin.infra.cda.ycf.YunChanFBJKVo;
import com.jy.rhin.model.ChanQianSF;
import com.jy.rhin.model.ClinicalDocumentData;
import com.jy.rhin.model.CodeObject;
import com.jy.rhin.model.body.fuyou.*;
import com.jy.rhin.model.header.Author;
import com.jy.rhin.model.header.Custodian;
import com.jy.rhin.model.header.Patient;
import com.jy.rhin.model.header.PatientRole;
import com.jy.rhin.utils.CDAUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class ChanQianSFApplication {

    @Resource
    private ChanQianSFService chanQianSFService;
    /*@Resource
    private DataMigrationAPI dataMigrationAPI;*/
    @Resource
    private CDACreator cdaCreator;

    @Resource
    private HospitalAPI hospitalAPI;

    public String create(String bh,String fUWUJGBH) {
        return cdaCreator.create(createFS(bh,fUWUJGBH), "06chanqiansf.ftl");
    }

    public ClinicalDocumentData createFS(String cHANHOU42TJKJCJLBH,String fUWUJGBH) {
        ChanQianSFVo info = chanQianSFService.queryOne(fUWUJGBH,cHANHOU42TJKJCJLBH);
        if(Objects.isNull(info)){
            throw new CDAException("主业务数据不存在",null);
        }

        HospitalDTO hospital = hospitalAPI.info(info.getFUWUJGBH()).getData();
//        HospitalDTO hospital = dataMigrationAPI.getByHospitalId(info.getFUWUJGBH()).getData();
        if(Objects.isNull(hospital)){
            throw new CDAException("机构数据不存在",null);
        }

        ChanQianSF docData = new ChanQianSF();
        setClinicalDocumentData(docData, info, hospital);
        
        docData.setJianKangPG(createJianKangPG(info));
        docData.setJianKangWT(createJianKangWT(info));
        docData.setJianKangZD(createJianKangZD(info));
        docData.setShengMingTZ(createShengMingTZ(info));
        docData.setXiaCiSF(createXiaCiSF(info));
        docData.setZhuanZhenJY(createZhuanZhenJY(info));
        docData.setChanQianSYS(createChanQianSYS(info));
        docData.setChanQianFZJC(createChanQianFZJC(info));
        docData.setChanQianJC(createChanQianJC(info));
        return docData;
    }
    private ChanQianJC createChanQianJC(ChanQianSFVo info){
    	ChanQianJC data = new ChanQianJC();
    	data.setFuWei(info.getFUWEI().setScale(1, BigDecimal.ROUND_HALF_UP).toString());
    	data.setGongDiGD(info.getGONGGAO().setScale(1, BigDecimal.ROUND_HALF_UP).toString());
    	data.setTaiFangW(new CodeObject(info.getTAIWEIDM(),info.getTAIWEIMC()));
    	data.setXinLv(info.getTAIXINL());
    	return data;
    }
    
    private ChanQianFZJC createChanQianFZJC(ChanQianSFVo info){
    	ChanQianFZJC data = new ChanQianFZJC();
    	data.setBChao(info.getBCHAOXS());
    	return data;
    }
    
    private ChanQianSYS createChanQianSYS(ChanQianSFVo info){
    	ChanQianSYS data = new ChanQianSYS();
    	data.setNiaoDanBJG(new CodeObject(info.getNIAODANBJGDM(),info.getNIAODANBJGMC()));
    	data.setXueHongDB(String.valueOf(info.getXUEHONGDB()));
    	return data;
    }
    
    private JianKangPG createJianKangPG(ChanQianSFVo info){
    	JianKangPG data = new JianKangPG();
    	data.setYiChangBZ("2".equals(info.getBAOJIANZDLBDM())?"true":"false");
    	data.setYiChangMS(info.getYUNCHANFYCJG());
    	return data;
    }
    
    private ChanQianSFJKWT createJianKangWT(ChanQianSFVo info){
    	ChanQianSFJKWT data = new ChanQianSFJKWT();
    	data.setYunZhou(String.valueOf(info.getSUIFANGYZT()));
    	data.setZhuSu(info.getZHUSU());
    	return data;
    }
    
    private List<ChanQianJianKangZD> createJianKangZD(ChanQianSFVo info){
    	List<ChanQianJianKangZD> list = new ArrayList<ChanQianJianKangZD>();
    	String[] dms = info.getBAOJIANZDLBDM().split("\\|");
    	for(int i=0;i<dms.length;i++){
    		ChanQianJianKangZD data = new ChanQianJianKangZD();
    		data.setZhiDao(new CodeObject(dms[i],info.getBAOJIANZDLBMC().split("\\|")[i]));
        	list.add(data);
    	}
    	return list;
    }
    
    
    private ChanQianSFShengMingTZ createShengMingTZ(ChanQianSFVo info){
    	ChanQianSFShengMingTZ data = new ChanQianSFShengMingTZ();
    	data.setShouSuoYa(String.valueOf(info.getSHOUSUOY()));
    	data.setShuZhangYa(String.valueOf(info.getSHUZHANGY()));
    	data.setTiZhong(info.getTIZHONG().setScale(2,BigDecimal.ROUND_HALF_UP).toString());
    	return data;
    }
    
    
    private ChanQianXiaCiSF createXiaCiSF(ChanQianSFVo info){
    	ChanQianXiaCiSF data = new ChanQianXiaCiSF();
    	data.setDiDian(info.getXIACICHANQIANJCDD());
    	data.setXiaCiSFSJ(CDAUtil.toD8(info.getXIACICHANQIANJQRQ()));
    	return data;
    }
    
    
    
    private FYZhuanZhenJY createZhuanZhenJY(ChanQianSFVo info){
    	FYZhuanZhenJY data = new FYZhuanZhenJY();
    	data.setZhuanZhenBZ("1".equals(info.getZHUANZHENBZ())?"true":"false");
    	data.setZhuanZhenYY(info.getZHUANZHENYY());
    	data.setZhuanRuJG(info.getZHUANRUJGMC());
    	data.setZhuanRuKS(info.getKESHIMC());
    	return data;
    }


    ClinicalDocumentData setClinicalDocumentData(ClinicalDocumentData clinicalDocumentData,ChanQianSFVo info, HospitalDTO hospital) {
        clinicalDocumentData.setEffectiveTime(CDAUtil.toD8(info.getCHANQIANJCRQ()));
        clinicalDocumentData.setPatientRole(createPatientRole(info));
        clinicalDocumentData.setAuthor(createAuthor(info, hospital));
        clinicalDocumentData.setCustodian(createCustodian(hospital));
        clinicalDocumentData.setId(String.valueOf(info.getId()));
        clinicalDocumentData.setVersionNumber("1.0");
        return clinicalDocumentData;
    }
    Custodian createCustodian(HospitalDTO hospital) {
        Custodian custodian = new Custodian();
        custodian.setId(hospital.getOutId());
        custodian.setName(hospital.getName());
        custodian.setAddr(hospital.getAddress());
        return custodian;
    }

    Author createAuthor(ChanQianSFVo info, HospitalDTO hospital) {
        Author author = new Author();
        author.setId("021");
        author.setTime(CDAUtil.toD8(info.getCHANQIANJCRQ()));
        author.setName(info.getSUIFANGYSXM());
        author.setOrganizationId(hospital.getOutId());
        author.setOrganizationName(hospital.getName());
        author.setOrganizationAddr(hospital.getAddress());
        return author;
    }

    PatientRole createPatientRole(ChanQianSFVo info) {
        PatientRole patientRole = new PatientRole();
        patientRole.setId(CDAUtil.formatNumber(info.getGERENDABS()));
        patientRole.setPatient(createPatient(info));
        return patientRole;
    }


    Patient createPatient(ChanQianSFVo info) {
        Patient patient = new Patient();
        YunChanFBJKVo bingrenxx = chanQianSFService.getBJK( info.getYUNCHANFWYBS());
        if(bingrenxx == null){
        	patient.setName("未知");
        }else{
        	patient.setName(bingrenxx.getXINGMING());
        }
        patient.setId(info.getZHENGJIANHM());
        return patient;
    }
}
